
package kaizone.haining.chaobohui.fragment;

import java.util.ArrayList;
import java.util.List;

import kaizone.haining.chaobohui.R;
import kaizone.haining.chaobohui.activity.CBHActivity;
import kaizone.haining.chaobohui.activity.DetailActivity;
import kaizone.haining.chaobohui.json.Data;
import kaizone.haining.chaobohui.json.EntityHuodong;
import kaizone.haining.chaobohui.json.Item;
import kaizone.haining.chaobohui.net.DataControl;
import kaizone.haining.chaobohui.util.Utils;
import kaizone.haining.chaobohui.widget.AdView;
import kaizone.haining.chaobohui.widget.CBHItemHuodong;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class CBHHuodongFragment extends Fragment implements DataControl.IDataUpdateListener,
        OnItemClickListener {

    public static final String DATA_SOURCE_URL = "http://api.dp-app.com/chao/remote/rest/query_activity_list.htm?page=";

    private FragmentManager mFragmentManager;

    private ArrayList<Item> mData;

    private HuodongAdapter mAdapter;

    private DataControl mDataControl;

    public CBHHuodongFragment() {
        init();
    }

    public CBHHuodongFragment(FragmentManager mgr) {
        mFragmentManager = mgr;
        init();
    }

    private void init() {
        mData = new ArrayList<Item>();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = (View) inflater.inflate(R.layout.cbh_fragment_huodong, null);

        TextView textView = (TextView) view.findViewById(R.id.tv01);
        String month = Utils.monthHZ();
        textView.setText(month);
        textView.setVisibility(View.GONE);

        ListView listview = (ListView) view.findViewById(R.id.listview);
        mAdapter = new HuodongAdapter();
        listview.setAdapter(mAdapter);
        // listview.setDivider(null);
        listview.setCacheColorHint(0);
        listview.setOnItemClickListener(this);
        initControl();
        initAd(view);
        return view;
    }

    public void initAd(View parent) {
        AdView adView = (AdView) parent.findViewById(R.id.adpanel);
        adView.loadAdSmall();
        adView.rangeWithPanel(getActivity());
    }

    private void initControl() {
        mDataControl = new DataControl(getActivity());
        mDataControl.addChangeListener(this);
        mDataControl.exec(DATA_SOURCE_URL + 1);
    }

    @Override
    public Object backParse(String datastring, String requeststring) {
        if (TextUtils.isEmpty(datastring))
            return null;
        EntityHuodong huodong = EntityHuodong.parseJson(datastring);
        return huodong;
    }

    @Override
    public void foreUpdate(Object result, String requeststring) {
        if (result == null)
            return;
        if (result instanceof EntityHuodong) {
            EntityHuodong huodong = (EntityHuodong) result;
            if (huodong != null) {
                Data data = huodong.getData();
                if (data == null)
                    return;
                List<Item> items = (List<Item>) data.getItems();
                mData.removeAll(mData);
                mData.addAll(items);
                mAdapter.notifyDataSetChanged();
            }
        }
    }

    @Override
    public void exception(Exception e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Object obj = mAdapter.getItem(position);
        // if (obj instanceof Serializable) {
        // DetailActivity.come(getActivity(), (Serializable) obj);
        // }
        if (obj instanceof Item) {
            Item item = (Item) obj;
            int requestid = item.getId();
            DetailActivity.come(getActivity(), DetailActivity.FLAG_HUODONG, requestid);
        }
    }

    private class HuodongAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return mData != null ? mData.size() : 0;
        }

        @Override
        public Object getItem(int position) {
            return mData != null ? mData.get(position) : 0;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            CBHItemHuodong cbhItemView = null;
            if (convertView == null) {
                convertView = (CBHItemHuodong) View.inflate(getActivity(), R.layout.item_huodong,
                        null);
                if (getActivity() instanceof CBHActivity) {
                    CBHActivity cbhActivity = (CBHActivity) getActivity();
                    ((CBHItemHuodong) convertView).setImageFetcher(cbhActivity.getImageFetcher());
                }
            }
            cbhItemView = (CBHItemHuodong) convertView;
            cbhItemView.setData(mData.get(position));
            //
            return cbhItemView;
        }
    }

    public static CBHHuodongFragment newInstance(FragmentManager fm) {
        final CBHHuodongFragment f = new CBHHuodongFragment(fm);
        return f;
    }
}
